package gitbook.dao;

import gitbook.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserRepository {

    @Autowired
    private DataSource dataSource;

    /**
     * 根据用户名查找
     *
     * @param userName
     * @return
     */
    public User findByUserName(String userName) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        User user = null;
        try {
            //查询数据
            connection = dataSource.getConnection();
            String sql = "select id,username,password,enabled from user where username = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, userName);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
//                user = resultSet.getObject(1,User.class);
                Integer id = resultSet.getInt(1);
                String username1 = resultSet.getString(2);
                String password = resultSet.getString(3);
                Boolean enabled = resultSet.getBoolean(4);
                user = new User(id, username1, password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if (!connection.isClosed()) {
                    connection.close();
                }
                if (!preparedStatement.isClosed()) {
                    preparedStatement.close();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return user;
    }

    /**
     * 根据用户名查询用户权限
     *
     * @param username
     * @return
     */
    public List<GrantedAuthority> findAuthoritiesByUsername(String username) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        List<GrantedAuthority> roleList = new ArrayList<>();
        try {
            //查询数据
            connection = dataSource.getConnection();
            String sql = "select role from user where username = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String roles = resultSet.getString(1);
                String[] roleArray = roles.split(",");
                for (String role : roleArray) {
                    SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role);
                    roleList.add(authority);
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if (!connection.isClosed()) {
                    connection.close();
                }
                if (!preparedStatement.isClosed()) {
                    preparedStatement.close();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return roleList;
    }

    public boolean findUserIsEnabled(String username) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = dataSource.getConnection();
            String sql = "select enabled from user where username=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                return resultSet.getBoolean(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if (!connection.isClosed()) {
                    connection.close();
                }
                if (!preparedStatement.isClosed()) {
                    preparedStatement.close();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 插入一条用户数据
     * @param spitter
     * @return
     */
//    public Integer save(Spitter spitter){
//        Connection connection = null;
//        PreparedStatement preparedStatement = null;
//        int modified=0;
//        try {
//            //插入数据
//            connection = dataSource.getConnection();
//            String sql = "insert into t_spitter(username,password,enabled) values(?,?,?)";
//            preparedStatement = connection.prepareStatement(sql);
//            preparedStatement.setString(1, spitter.getUserName());
//            preparedStatement.setString(2, spitter.getPassword());
//            preparedStatement.setBoolean(3, spitter.getEnabled());
//            modified = preparedStatement.executeUpdate();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            try {
//                if(!connection.isClosed()){
//                    connection.close();
//                }
//                if(!preparedStatement.isClosed()) {
//                    preparedStatement.close();
//                }
//            } catch (Exception e1) {
//                e1.printStackTrace();
//            }
//        }
//        return modified;
//    }

}
